<?php 
if (!defined('BASEPATH')){
	exit('No direct script access allowed');
}

/**
 * refundTicket Controller
 * Build by Phuc Nguyen
 * Contact : nguyenvanphuc0626@gmail.com
 */

class RefundTicket extends BACKEND_Controller {
	public function __construct() {
		parent::__construct();
		$this->load->language('refundTicket');
		$this->load->language('button');
		if($this->database_connect_status){
			$this->load->model('refundTicket_model');
			$this->set_controller('refundTicket');
			$this->set_model($this->refundTicket_model);
		}
                $this->load->model('refundTicket_model');

                $this->load->library('bookinglib');
                $this->bookinglib = new bookinglib();
	}
        
        public function view(){
            $params = $this->input->get('id');
            if($params){
                $info   =   $this->refundTicket_model->find_by(array('id'=>$params));
                $this->view_data['list']    =   $info[0];
                $this->load->view('auth/refundTicket/view', $this->view_data);
            }
        }

        public function cronPrice(){
            if($this->input->server('REQUEST_METHOD')=='POST'){
                $this->load->model('booking_model');
                $this->load->model('bookhistory_model');
                
                $info = $this->booking_model->find_by(array('booking_place_code'=>$this->input->post('booking_place_code'),'airline'=>$this->input->post('airline')));
                if(empty($info)){
                    $info = $this->bookhistory_model->find_by(array('booking_place_code'=>$this->input->post('booking_place_code'),'airline'=>$this->input->post('airline')));
                }
                
                die(json_encode($info[0]));
            }
        }
        
        public function accept(){
            if($this->input->server('REQUEST_METHOD')=='POST'){
                $arr        = $this->refundTicket_model->find_by(array('id'=>$this->input->post('id')));
                $update     = false;
                if($this->input->post('checkbox_booker')){
//                    if($this->input->post('loinhuan_booker_fake')>0){
                        $this->view_data["refundTicket"]                        = new stdClass();
                        $this->view_data["refundTicket"]->customerID            = $arr[0]->customerID;
                        $this->view_data["refundTicket"]->phone                 = $arr[0]->phone;
                        $this->view_data["refundTicket"]->airline               = json_encode(array($arr[0]->airline));
                        $this->view_data["refundTicket"]->booking_place_code    = json_encode(array($arr[0]->booking_place_code));
                        $this->view_data["refundTicket"]->booking_types         = 0;
                        $this->view_data["refundTicket"]->createdTime           = 0;
                        $this->view_data["refundTicket"]->net                   = 0;
                        $this->view_data["refundTicket"]->sale                  = 0;
                        $this->view_data["refundTicket"]->net_fare              = 0;
                        $this->view_data["refundTicket"]->sale_fare             = 0;
                        $this->view_data["refundTicket"]->real_fare             = $this->input->post('loinhuan_booker_fake');
                        $this->view_data["refundTicket"]->note_bk               = '';
                        $this->view_data["refundTicket"]->note_pay              = '[DuyetLN] Lợi nhuận booker';
                        $this->view_data["refundTicket"]->payment_method        = $arr[0]->payment_method;
                        $this->view_data["refundTicket"]->payment_confirm       = 1;
                        $this->view_data["refundTicket"]->refundticket_id       = $this->input->post('id');
                        $this->view_data["refundTicket"]->owner                 = $arr[0]->owner;//
                        $this->view_data["refundTicket"]->agent_code            = $this->session->userdata['agent_code'];
                        $this->view_data["refundTicket"]->lastupdated           = date("Y-m-d H:i:s",time());
                        
                        $this->load->model('voucherHistory_model');
                        $result = $this->voucherHistory_model->create($this->view_data["refundTicket"]);
                        if(!$result){
                            $paramAdminLog  = array(
                                'userid'            => $this->session->userdata['user_id'],
                                'lastLogin'         => date('Y-m-d :H:i:s',time()),
                                'ip'                => $_SERVER['REMOTE_ADDR'],
                                'logAction'         => '[DuyetHVFail] code='.$arr[0]->booking_place_code.', hãng='.$arr[0]->airline.'result=',json_decode($result),
                                'agent_code'        => $this->session->userdata['agent_code']
                            );
                            $this->user_model->insertUserAdminLog($paramAdminLog);
                            die("Update loi nhuan booker that bai! Liên hệ Admin");
                        }
                        $update = true;
                    //}
                }
                
                if($this->input->post('checkbox_khach')){
                    $this->load->model('listcustomers_model');
                    $this->load->model('voucher_model');
                    $this->load->model('voucherPaymentLog_model');
                    $result = $this->listcustomers_model->updateSurplusTicket($arr[0]->customerID,$this->input->post('sodu_khach_fake'));
                    //push hoan ve vao chung tu cong no duong
                        $this->view_data["voucherLunisolar"]                        = new stdClass();
                        $this->view_data["voucherLunisolar"]->customerID            = $arr[0]->customerID;
                        $this->view_data["voucherLunisolar"]->phone                 = $arr[0]->phone;
                        $this->view_data["voucherLunisolar"]->airline               = json_encode(array($arr[0]->airline));
                        $this->view_data["voucherLunisolar"]->booking_place_code    = json_encode(array($arr[0]->booking_place_code));
                        $this->view_data["voucherLunisolar"]->booking_types         = 0;
                        $this->view_data["voucherLunisolar"]->createdTime           = 0;
                        $this->view_data["voucherLunisolar"]->net                   = 0;
                        $this->view_data["voucherLunisolar"]->sale                  = 0;
                        $this->view_data["voucherLunisolar"]->net_fare              = 0;
                        $this->view_data["voucherLunisolar"]->sale_fare             = 0;
                        $this->view_data["voucherLunisolar"]->real_fare             = 0;
                        $this->view_data["voucherLunisolar"]->residual              = $this->input->post('sodu_khach_fake');
                        $this->view_data["voucherLunisolar"]->note_bk               = '';
                        $this->view_data["voucherLunisolar"]->note_pay              = '[FocusHV] Công nợ dương KH';
                        $this->view_data["voucherLunisolar"]->checkbox_sodukh       = 1;
                        $this->view_data["voucherLunisolar"]->payment_method        = $arr[0]->payment_method;
                        $this->view_data["voucherLunisolar"]->payment_confirm       = 1;
                        $this->view_data["voucherLunisolar"]->refundticket_id       = $this->input->post('id');
                        $this->view_data["voucherLunisolar"]->owner                 = $arr[0]->owner;//
                        $this->view_data["voucherLunisolar"]->agent_code            = $this->session->userdata['agent_code'];
                        $this->view_data["voucherLunisolar"]->lastupdated           = date("Y-m-d H:i:s",time());
                        
                        $this->load->model('voucherLunisolar_model');
                        $result = $this->voucherLunisolar_model->create($this->view_data["voucherLunisolar"]);
                        
                         //insert payment log
                        $this->view_data["voucherPaymentLog"]                               = new stdClass();
                        $this->view_data["voucherPaymentLog"]->customerID                   = $arr[0]->customerID;
                        $this->view_data["voucherPaymentLog"]->voucher_id                   = $result;
                        $this->view_data["voucherPaymentLog"]->amount                       = 0;
                        $this->view_data["voucherPaymentLog"]->surplus                      = $this->input->post('sodu_khach_fake');
                        $this->view_data["voucherPaymentLog"]->residual                     = $this->input->post('sodu_khach_fake');
                        $this->view_data["voucherPaymentLog"]->note                         = '[FocusHV] Công nợ dương';
                        $this->view_data["voucherPaymentLog"]->agent_code                   = $this->session->userdata['agent_code'];
                        $this->view_data["voucherPaymentLog"]->owner                        = $arr[0]->owner;
                        $this->view_data["voucherPaymentLog"]->lastupdated                  = time();

                        $log = $this->voucherPaymentLog_model->create($this->view_data["voucherPaymentLog"]);
                        unset($this->view_data["voucherPaymentLog"]);
                        if(!$result || !$log){
                                $paramAdminLog  = array(
                                        'userid'            => $this->session->userdata['user_id'],
                                        'lastLogin'         => date('Y-m-d :H:i:s',time()),
                                        'ip'                => $_SERVER['REMOTE_ADDR'],
                                        'logAction'         => '[DuyetHVFail] code='.$arr[0]->booking_place_code.', hãng='.$arr[0]->airline.'result='.json_decode($result),
                                        'agent_code'        => $this->session->userdata['agent_code']
                                );
                                $this->user_model->insertUserAdminLog($paramAdminLog);
                                die("Update so du khach hang vao Chung tu cong no duong that bai! Lien he Admin");
                        }

                    //end push
                    $update = true;
                }
                
                if($update){
                    $this->session->set_flashdata('flash_message', $this->lang->line('update_successful'));
                    $this->view_data["updatePamras"]                            = new stdClass();
                    $this->view_data["updatePamras"]->status                    = 1;
                    $this->view_data["updatePamras"]->note_pay                  = trim($this->input->post('note_pay'));
                    $this->refundTicket_model->update($this->view_data["updatePamras"], $this->input->post('id'));
                    //adminlog
                    $paramAdminLog  = array(
                            'userid'            => $this->session->userdata['user_id'],
                            'lastLogin'         => date('Y-m-d :H:i:s',time()),
                            'ip'                => $_SERVER['REMOTE_ADDR'],
                            'logAction'         => '[DuyetHVSuccess] code='.$arr[0]->booking_place_code.', hãng='.$arr[0]->airline,
                            'agent_code'        => $this->session->userdata['agent_code']
                    );
                    $this->user_model->insertUserAdminLog($paramAdminLog);
                    die("1");
                } else {
                    die("0");
                }
                
            }
            
            $refundTicket_query                     = $this->refundTicket_model->find_by(array('id'=>$this->input->get('id')));
            $this->view_data['refundTicket']        = $refundTicket_query[0];
            
            $this->load->view('auth/refundTicket/edit', $this->view_data);
        }
        
        protected function update($params=NULL){
		if($this->input->server('REQUEST_METHOD')=='POST'){
                        $this->view_data["refundTicket"]                                   = new stdClass();
                        $this->view_data["refundTicket"]->phone                            = $this->input->post('phone');
                        $this->view_data["refundTicket"]->airline                          = $this->input->post('airline');
                        $this->view_data["refundTicket"]->booking_place_code               = json_encode($this->input->post('booking_place_code'));
                        $this->view_data["refundTicket"]->customerID                       = $this->input->post('customerID');
                        $this->view_data["refundTicket"]->net_fare                         = $this->input->post('net_fare_fake');
                        $this->view_data["refundTicket"]->sale_fare                        = $this->input->post('sale_fare_fake');
                        $this->view_data["refundTicket"]->thuctra                          = $this->input->post('thuctra_fake');
                        $this->view_data["refundTicket"]->phihoantra                       = $this->input->post('phihoantra_fake');
                        $this->view_data["refundTicket"]->payment_method                   = $this->input->post('payment_method');
                        $this->view_data["refundTicket"]->payment_info                     = $this->input->post('choicebank')?$this->input->post('choicebank'):'';
                        $this->view_data["refundTicket"]->note                             = trim($this->input->post('note'));
                        $this->view_data["refundTicket"]->agent_code                       = $this->session->userdata('agent_code');
                        $this->view_data["refundTicket"]->owner                            = $this->session->userdata('user_id');
                        $this->view_data["refundTicket"]->lastupdated                      = date("Y-m-d H:i:s",time());

                        $this->load->helper('form');
                        $this->load->helper('character');
                        $this->load->helper('form');
                        $this->load->library('form_validation');
                        $this->form_validation->set_error_delimiters('<div class="alert alert-error"><strong>'.$this->lang->line('error').': </strong>', '</div>');
                        $rules = array(
                            array(
                                'field'   => 'phone',
                                'label'   =>  $this->lang->line('phone'),
                                'rules'   => 'required|numberic|xss_clean'
                            ),array(
                                'field'   => 'airline',
                                'label'   =>  $this->lang->line('airline'),
                                'rules'   => 'required|trim|max_length[2]|xss_clean'
                            ),array(
                                'field'   => 'net_fare',
                                'label'   =>  $this->lang->line('net_fare'),
                                'rules'   => 'required|numberic|xss_clean'
                            ),array(
                                'field'   => 'sale_fare',
                                'label'   =>  $this->lang->line('net_fare'),
                                'rules'   => 'required|numberic|xss_clean'
                            ),array(
                                'field'   => 'thuctra',
                                'label'   =>  $this->lang->line('thuctra'),
                                'rules'   => 'required|trim|xss_clean'
                            ),array(
                                'field'   => 'phihoantra',
                                'label'   =>  $this->lang->line('phi-hoan-tra'),
                                'rules'   => 'trim|xss_clean'
                            ),array(
                                'field'   => 'note',
                                'label'   =>  $this->lang->line('phi-hoan-tra'),
                                'rules'   => 'trim|max_length[255]|xss_clean'
                            )
                        );

                        $this->form_validation->set_error_delimiters('<div class="alert alert-error"><strong>'.$this->lang->line('error').': </strong>', '</div>');
                        $this->form_validation->set_rules($rules);

                        if ($this->form_validation->run()==TRUE){
                                if($params){
					$this->refundTicket_model->update($this->view_data["refundTicket"], $params);
                                        $logAction                              = '[UpdateRefundTicket] '.$this->lang->line('update_successful');
				}else{
					$params                                 = $this->refundTicket_model->create($this->view_data["refundTicket"]);
                                        $logAction                              = '[AddRefundTicket] '.$this->lang->line('update_successful');
				}
                                
				if($params){
					$this->session->set_flashdata('flash_message', $this->lang->line('update_successful'));
					$paramAdminLog  = array(
                                            'userid'            => $this->session->userdata['user_id'],
                                            'lastLogin'         => date('Y-m-d :H:i:s',time()),
                                            'ip'                => $_SERVER['REMOTE_ADDR'],
                                            'logAction'         => $logAction,
                                            'agent_code'        => $this->session->userdata['agent_code']
                                        );
                                        $this->user_model->insertUserAdminLog($paramAdminLog);
                                        redirect('auth/refundTicket');
				}
                        }
            }
            
            $this->load->model('refundTicket_model');
            $this->load->model('language_model');

            if(isset($params)){
                    $refundTicket_query	= $this->refundTicket_model->find_by(array('id'=>$params));
                    
                    if(!isset($refundTicket_query[0])){
                            $this->session->set_flashdata('flash_message', $this->lang->line('not_exists'));
                            redirect(site_url('auth/refundTicket'));
                            exit();
                    }

                    $this->view_data['refundTicket']				= $refundTicket_query[0];
            }

            $this->view_data['js'] = array(
                            base_url().'static/templates/backend/js/main.js'
            );
            $this->view_data['css'] = array(
            );
            $this->load->view('auth/refundTicket/edit', $this->view_data);
	}
        
}

/* End of file refundTicket.php */
/* Location: ./application/controllers/auth/refundTicket.php */